package com.wx.wxpush.controller;

/**
 *@ClassName PushController
 *@Description TODO
 *@Author chen
 *@Date 2022/8/22 09:54
 */

import com.wx.wxpush.entiy.TempData;
import com.wx.wxpush.entiy.TemplateData;
import com.wx.wxpush.mapper.CornTaskMapper;
import com.wx.wxpush.mapper.PusherMapper;
import com.wx.wxpush.service.PusherService;
import com.wx.wxpush.util.AddressUtils;
import com.wx.wxpush.util.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

import static com.wx.wxpush.util.CronUtil.getCronto;


@Slf4j
@Controller
@RequestMapping("/wxpush")
public class  PushController {
    @Autowired
    private PusherService pusherService;
    @Autowired
    private PusherMapper pusherMapper;
    @Autowired
    private CornTaskMapper cornTaskMapper;
    //要推送的用户openid
    //private static String openid = "xxx";

    @PostMapping("/push")
    @ResponseBody
        public ResultVO pusher(@RequestBody TempData templateData,HttpServletRequest request){
        String ipAddress = AddressUtils.getIpAddr(request);
        String address=AddressUtils.getAddressByIP(ipAddress);
        System.out.println(ipAddress+address+"ip地址");
        //福建省厦门市 电信
        String ipadd=address.substring(0,5);
        templateData.setIpAddress(ipadd);
        if(templateData.getOpenid()==null || templateData.getOpenid().equals("")){
            return ResultVO.fail("传进来的openid为空");
        }
        String s= pusherMapper.selectByOpenid(templateData.getOpenid());
        if(templateData.getCornnight()!=null || templateData.getCronmoring() !=null
                ||  templateData.getMoringStatus()!=null||templateData.getNightStatus()!=null

        ){
            System.out.println("te"+templateData.getCronmoring());
            cornTaskMapper.updateByTenp(templateData);
            //return ResultVO.ok();
        }
        if(s==null){
        pusherMapper.pushmorning(templateData);}
        else {
        pusherMapper.updateByOpenid(templateData);
        }
       // int i=pusherService.pusher(templateData);

        return ResultVO.ok("ok");
    }

//
//    /**
//     * 微信测试账号推送今天
//     *
//     */
//    @GetMapping("/push/today")
//    public void push() {
//        Pusher.pushMorning(openid);
//    }
//
//    /**
//     * 微信测试账号推送明天
//     * */
//    @GetMapping("/push/tomorrow")
//    public void pushZyd() {
//        Pusher.pushNight(openid);
//    }

}